import pygame, os, sys
import conversation, tweet

class ConversationSurface(pygame.sprite.Sprite):
	def __init__(self, displayCon):
		self.displayCon = displayCon
		self.cur_ticks = pygame.time.get_ticks()
		self.yspeed = 0
		self.save_yincr = 0
		self.background = pygame.Surface((1024,768))
		self.background.fill((82 , 219, 234))
		self.slowDown = False
		self.drawRect = pygame.Rect(0, 0, 1024, 768)
		self.numTweets = len(self.displayCon.tweetList)
		tweetSurList = []
		for tweet in self.displayCon.tweetList:
			tweetSurList.append(tweet.getSurface())
		yPos = 0
		surfaceHeight = tweetSurList[0].get_height() * len(tweetSurList)
		self.image = pygame.Surface((1024, surfaceHeight))
		self.rect = self.image.get_rect()

		for tweetSur in tweetSurList:
			self.image.blit(tweetSur, (0, yPos))
			yPos += tweetSur.get_height()

	def draw(self, screen):
		screen.blit(self.background, (0,0))
		#screen.blit(self.image, self.rect.topleft, self.drawRect)
		screen.blit(self.image, (0,0), self.drawRect)

	def update(self):
		if self.numTweets != len(self.displayCon.tweetList):
			newConScreen = self.displayCon.getConverSurface()
			self.image = newConScreen.image
			self.numTweets = newConScreen.numTweets
		#print self.slowDown
		if self.slowDown:
			self.yspeed *= .97
		prev_ticks = self.cur_ticks
		self.cur_ticks = pygame.time.get_ticks()
		ticks = self.cur_ticks - prev_ticks
		yincr = self.yspeed * ticks
		# Save the decimal part of incr
		self.save_yincr += yincr - int(yincr)
		# Use only the integer part of incr
		yincr = int(yincr)
		
		newpos = None
		if abs(yincr) >= 1:        
			#newpos = self.rect.move(0, yincr)
			newpos = self.drawRect.move(0, yincr)
			#self.rect = newpos
			self.drawRect = newpos
		if abs(self.save_yincr) >= 1.0:
			# Use only the integer part of the new incr
			yincr = int(self.save_yincr)
			# Save the decimal part of self.save_incr
			self.save_yincr = self.save_yincr - yincr
			#newpos = self.rect.move(0, yincr)
			newpos = self.drawRect.move(0, yincr)
			#self.rect = newpos
			self.drawRect = newpos
		#self.x, self.y = self.rect.topleft
		self.x, self.y = self.drawRect.topleft
		
		if self.y < 0:
			self.drawRect.top = 0
		if self.drawRect.bottom > self.image.get_rect().height + 120:
			self.drawRect.bottom = self.image.get_rect().height + 120

	def moveUp(self):
		self.yspeed = -.3
		self.slowDown = False

	def moveDown(self):
		self.yspeed = .3
		self.slowDown = False

	def stop(self):
		#self.yspeed = 0
		self.slowDown = True

